package org.example.backend.service.impl;

import org.example.backend.dto.StatisticsDTO;
import org.example.backend.mapper.StatisticsMapper;
import org.example.backend.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

/**
 * 数据统计服务实现类
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Override
    public StatisticsDTO getStatistics() {
        // 获取当前日期
        Date now = new Date();
        
        // 获取今日开始时间
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(now);
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        
        // 获取今日结束时间
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.setTime(now);
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        
        // 获取7天前的日期
        Calendar sevenDaysAgo = Calendar.getInstance();
        sevenDaysAgo.setTime(now);
        sevenDaysAgo.add(Calendar.DAY_OF_MONTH, -6); // 包括今天，所以是-6
        sevenDaysAgo.set(Calendar.HOUR_OF_DAY, 0);
        sevenDaysAgo.set(Calendar.MINUTE, 0);
        sevenDaysAgo.set(Calendar.SECOND, 0);
        sevenDaysAgo.set(Calendar.MILLISECOND, 0);
        
        // 构建统计数据
        StatisticsDTO statisticsDTO = new StatisticsDTO();
        
        // 基础统计数据
        statisticsDTO.setUserCount(statisticsMapper.selectUserCount());
        statisticsDTO.setOrderCount(statisticsMapper.selectOrderCount(null, null));
        statisticsDTO.setTotalSales(statisticsMapper.selectTotalSales(null, null));
        statisticsDTO.setProductCount(statisticsMapper.selectProductCount());
        
        // 今日统计数据
        statisticsDTO.setTodayOrderCount(statisticsMapper.selectOrderCount(todayStart.getTime(), todayEnd.getTime()));
        statisticsDTO.setTodaySales(statisticsMapper.selectTotalSales(todayStart.getTime(), todayEnd.getTime()));
        
        // 待处理订单数
        statisticsDTO.setPendingOrderCount(statisticsMapper.selectPendingOrderCount());
        
        // 最近7天每日订单数和销售额
        statisticsDTO.setOrderCountByDay(statisticsMapper.selectOrderCountByDay(sevenDaysAgo.getTime(), todayEnd.getTime()));
        statisticsDTO.setSalesByDay(statisticsMapper.selectSalesByDay(sevenDaysAgo.getTime(), todayEnd.getTime()));
        
        // 各状态订单数量
        statisticsDTO.setOrderCountByStatus(statisticsMapper.selectOrderCountByStatus());
        
        // 热销商品排行（前10）
        statisticsDTO.setHotProducts(statisticsMapper.selectHotProducts(10));
        
        return statisticsDTO;
    }

    @Override
    public StatisticsDTO getStatisticsByDateRange(Date startDate, Date endDate) {
        // 构建统计数据
        StatisticsDTO statisticsDTO = new StatisticsDTO();
        
        // 基础统计数据
        statisticsDTO.setUserCount(statisticsMapper.selectUserCount());
        statisticsDTO.setOrderCount(statisticsMapper.selectOrderCount(startDate, endDate));
        statisticsDTO.setTotalSales(statisticsMapper.selectTotalSales(startDate, endDate));
        statisticsDTO.setProductCount(statisticsMapper.selectProductCount());
        
        // 今日统计数据和待处理订单数保持不变
        Date now = new Date();
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(now);
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.setTime(now);
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        
        statisticsDTO.setTodayOrderCount(statisticsMapper.selectOrderCount(todayStart.getTime(), todayEnd.getTime()));
        statisticsDTO.setTodaySales(statisticsMapper.selectTotalSales(todayStart.getTime(), todayEnd.getTime()));
        statisticsDTO.setPendingOrderCount(statisticsMapper.selectPendingOrderCount());
        
        // 指定日期范围的每日订单数和销售额
        statisticsDTO.setOrderCountByDay(statisticsMapper.selectOrderCountByDay(startDate, endDate));
        statisticsDTO.setSalesByDay(statisticsMapper.selectSalesByDay(startDate, endDate));
        
        // 各状态订单数量
        statisticsDTO.setOrderCountByStatus(statisticsMapper.selectOrderCountByStatus());
        
        // 热销商品排行（前10）
        statisticsDTO.setHotProducts(statisticsMapper.selectHotProducts(10));
        
        return statisticsDTO;
    }
} 